/*
 * Copyright 2017 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.controller;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.util.SavedRequest;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.LoginAO;
import com.lap.scp.admin.ao.ResourcesAO;
import com.lap.scp.admin.domain.LoginDO;
import com.lap.scp.admin.domain.UserDO;

/**
 * <pre>
 *
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2017年9月3日 下午2:39:05
 * @since 1.0
 */
@Controller
public class LoginController {

	@Resource
	private HttpServletRequest request;

	@Resource
	private HttpServletResponse response;

	@Resource
	private ServletContext servletContext;

	@Resource
	private LoginAO loginAO;

	@Resource
	private ResourcesAO resourcesAO;

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public ModelAndView login() {
		ModelAndView andView = new ModelAndView("login");
		andView.addObject("loginDO", new LoginDO());
		return andView;
	}

	@RequestMapping(value = "login", method = RequestMethod.POST)
	public ModelAndView loginCheck(LoginDO loginDO) {
		ModelAndView andView = new ModelAndView();

		Resp<UserDO> resp = loginAO.login(loginDO);
		if (resp.isSuccess()) {
			UserDO userDO = resp.getData();
			andView.addObject("userDO", userDO);

			UsernamePasswordToken token = new UsernamePasswordToken(loginDO.getUsername(), loginDO.getPassword());
			token.setRememberMe(loginDO.getRememberMe());

			Subject subject = SecurityUtils.getSubject();
			subject.login(token);
			redirectPreviousPage(andView, userDO);
		} else {
			andView.setViewName("login");
			andView.addObject("result", resp);
			andView.addObject("loginDO", loginDO);
		}
		return andView;
	}

	private void redirectPreviousPage(ModelAndView andView, UserDO userDO) {
		SavedRequest savedRequest = WebUtils.getSavedRequest(request);
		if (savedRequest != null) {
			String url = savedRequest.getRequestUrl();
			if (StringUtils.isNotBlank(url) && !"/".equals(url)) {
				String contextPath = servletContext.getContextPath();
				andView.setViewName("redirect:" + url.replaceFirst(contextPath, ""));
				return;
			}
		}
		andView.setViewName("redirect:user/profile");
	}

	@RequestMapping(value = "secu", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView secu() {
		return new ModelAndView("secu");
	}

}
